package chatGUI;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import people.User;

/**
 * <b>Project ChatSystemG2 - INSA 4INFOA1 - 2013/2014</b>
 * <P>
 * <i>Package  chatGUI</i> <br>
 * ChatTab is a part of a WindowComunication.
 * <br>
 * One chatTab by conversation with a user is opened.
 * <br>
 * It implements the interface ActionListeners and extends the class JPanel.
 * <br>
 * It contains the following attributes: the chatGUI, a title, the user corresponding to the conversation, a button to send text and a button to send a file, a text field and a text area, a file chooser and a button panel.
 *
 * @author Sergio Gonzalez - Marie-Charlotte Bideaud
 */

public class ChatTab extends JPanel implements ActionListener{

	private static final long serialVersionUID = 1L;
	
	private ChatGUI cgui;
	private String title;
	private User user;
	private JButton btnSend;
	private JButton btnFile;
	private JTextField text;
	private JTextArea textArea;

	private JFileChooser fc;
	private JPanel buttonPanel;

	/**
	 * Constructor for the class ChatTab
	 * 
	 * @param title : String 
	 * @param user : User 
	 * @param cgui : ChatGUI
	 */
	public ChatTab (String title, User user, ChatGUI cgui) {
		this.cgui = cgui;
		this.title = title;
		this.setUser(user);
		initPanel();
	}
	
	/**
	 * Method to initiate a tab (called when a new ChatTab is instanced). 
	 */
	public void initPanel(){
		// Definition of the layouts
		this.setLayout(new BorderLayout(0,0));
		this.setSize(new Dimension(200, 300));
		this.setMinimumSize(new Dimension(200, 300));
		
		// Definition of the text area (where the sent and received messages appear)
		textArea = new JTextArea(25,30);
        textArea.setMargin(new Insets(5,5,5,5));
        textArea.setEditable(false);

        // Components useful to communicate : a text field, and the 2 buttons (text and file)
        text = new JTextField();
        text.setColumns(20);
        text.addActionListener(this);
        text.setActionCommand("Send");
        
        btnFile = new JButton("File");
        btnFile.addActionListener(this);
        btnFile.setActionCommand("File");
        btnSend = new JButton("Send");
        btnSend.addActionListener(this);
        btnSend.setActionCommand("Send");
        
        // Create a file chooser
        fc = new JFileChooser();

        // For layout purposes, put the buttons in a separate panel
        buttonPanel = new JPanel(); 
        buttonPanel.add(text, FlowLayout.LEFT);
        buttonPanel.add(btnSend, FlowLayout.CENTER);
        buttonPanel.add(btnFile, FlowLayout.RIGHT);
        
        // Add the buttons and the log to this panel.
		this.add(textArea, BorderLayout.NORTH);
		this.add(new JScrollPane(textArea));
		this.add(buttonPanel, BorderLayout.SOUTH);
		focusText();
	}

	/**
	 * Getter for the title of the tab
	 * 
	 * @return String
	 */
	public String getTitle() {
		return title;
	}

	/**
	 * Setter of the title of the tab
	 * 
	 * @param title : String
	 */
	public void setTitle(String title) {
		this.title = title;
	}

	/**
	 * Method called when a button is pressed (interface ActionListener)
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		// if the user wants to send a file (the btnfile is pressed) 
		if (e.getActionCommand().equals("File")) {
            int returnVal = fc.showOpenDialog(ChatTab.this);
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                File file = fc.getSelectedFile();
                // Send the file request.
                cgui.clickSendFile(file, this.getUser());
                textArea.append("--> OPENING: " + file.getName() + "." + "\n");
            }
            textArea.setCaretPosition(textArea.getDocument().getLength());
        }
		// if the user wants to send a text message 
		if (e.getActionCommand().equals("Send")) {
			if (!text.getText().equals("")) {
				cgui.clickSendMessageButton(text.getText(), user, textArea);
				text.setText("");
			}
		}
	}
	
	/**
	 * Getter of the user
	 * 
	 * @return the user
	 */
	public User getUser() {
		return user;
	}

	/**
	 * Setter of the user
	 * 
	 * @param user : User 
	 */
	public void setUser(User user) {
		this.user = user;
	}
	
	/**
	 * Getter of the text area
	 * 
	 * @return JTextArea
	 */
	public JTextArea getTextArea() {
		return textArea;
	}
	
	/**
	 * Method requestFocusInWindow of javax.swing.JComponent
	 */
	public void focusText () {
		text.requestFocusInWindow();
	}
	
}
